package com.jackstraw.jack_base.datasource.database.parten.aspect.contextholder;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.jackstraw.jack_base.datasource.common.entity.enums.RWEnum;
import com.jackstraw.jack_base.util.ObjectUtil;

/**
 * 数据库读写
 */
public class DataBaseRWContextHolder {

    /**
     * db读写
     */
    private static final ThreadLocal<RWEnum> dbRWContextHolder = new TransmittableThreadLocal<>();

    /**
     * 设置数据库读写
     *
     * @param dbRW 数据源读写类型
     */
    public static void set(RWEnum dbRW) {
        if (ObjectUtil.ObjectUtilByLang3.isNotEmpty(dbRW)) {
            dbRWContextHolder.set(dbRW);
        }
    }


    /**
     * 得到数据库读写
     */
    public static RWEnum get() {
        return dbRWContextHolder.get();
    }

    /**
     * 清空数据库读写
     */
    public static void clear() {
        dbRWContextHolder.remove();
    }
}
